unit uCommonScript;

interface

uses Classes, uKeyboardMouse, uParamsController, uConstants, SysUtils;

type
    TActionObject = class (TObject)
    private
        FPriority: byte;
        FActionType: byte;
        FTickCount: byte;
        FDelay: integer;
        procedure SetPriority(Priority: byte);
        procedure SetActionType(ActionType: byte);
        procedure SetTickCount(TickCount: byte);
    public
        constructor Create(ActionType,Priority: byte; Delay: integer);
        destructor Destroy;
        procedure ExecAction;
        property Priority: byte read FPriority write SetPriority;
        property ActionType: byte read FActionType write SetActionType;
        property TickCount: byte read FTickCount write SetTickCount;
        property Delay: integer read FDelay write FDelay;
    end;

type
    TCommonScript = class (TObject)
    protected
        FActionList: TList;
        FLastUse: array [0..23] of TDateTime;
        FReUseTime: array [0..23] of TDateTime;
        procedure SelfControll; dynamic;
        procedure AnalisData; dynamic;
        procedure PvPControll; dynamic;
        procedure AddNewAction (ActionType, Priority: byte; Delay: integer = 0);
        procedure SortActionList;
        procedure ExecuteActions;
        procedure SetArrayData;
        procedure AutoRes;
    public
        constructor Create;
        destructor Destroy;
        procedure LaunchScript; dynamic;
    end;

implementation

uses uMainData, fMain;


constructor TActionObject.Create(ActionType, Priority: byte; Delay: integer);
begin
    FActionType := ActionType;
    FPriority := Priority;
    FTickCount := 0;
    FDelay := Delay;
end;

destructor TActionObject.Destroy;
begin
    inherited;
end;

procedure TActionObject.SetPriority(Priority: byte);
begin
    FPriority := Priority;
end;

procedure TActionObject.SetActionType(ActionType: byte);
begin
    FActionType := ActionType;
end;

procedure TActionObject.SetTickCount(TickCount: byte);
begin
    FTickCount := TickCount;
end;

procedure TActionObject.ExecAction;
begin
    KeyController.ActionToKey(FActionType);
    Sleep(FDelay);
end;

constructor TCommonScript.Create;
begin
    inherited;
    FActionList := TList.Create;
    SetArrayData;
end;

procedure TCommonScript.SetArrayData;
var
    i: integer;
begin
    for i := 0 to High(FReUseTime) do
    begin
        FReUseTime[i] := ReUseTime[i];
    end;
end;

destructor TCommonScript.Destroy;
begin
    inherited;
    FActionList.Free;
end;

procedure TCommonScript.LaunchScript;
begin
    AnalisData;
    SortActionList;
    ExecuteActions;
end;

procedure TCommonScript.SelfControll;
begin
//CP
    if (TParamsController(MainData.PartyStateList.ParamsList.Items[0]).CP < cpLvlNormal) then
        AddNewAction (atDrinkGCP, plImmediately);
    if (TParamsController(MainData.PartyStateList.ParamsList.Items[0]).CP < cpLvlMiddle) then
        AddNewAction (atDrinkBCP, plImmediately);

//HP
    if (TParamsController(MainData.PartyStateList.ParamsList.Items[0]).HP < hpLvlNormal) then
            AddNewAction (atDrinkLHP, plImmediately);
    if (TParamsController(MainData.PartyStateList.ParamsList.Items[0]).HP < hpLvlHeight) then
            AddNewAction (atDrinkWHP, plImmediately);
    if (TParamsController(MainData.PartyStateList.ParamsList.Items[0]).HP < hpLvlMiddle) then
            AddNewAction (atDrinkQHP, plImmediately);
    if TParamsController(MainData.PartyStateList.ParamsList.Items[0]).HP < hpLvlLow then
    begin
            AddNewAction (atDrinkQHP, plImmediately);
            AddNewAction (atDrinkBHP, plImmediately);
    end;

//MP
    if TParamsController(MainData.PartyStateList.ParamsList.Items[0]).MP < mpLvlLow then
            AddNewAction(atDrinkMP, plImmediately);
end;

procedure TCommonScript.PvPControll;
begin
//CP
    if (TParamsController(MainData.PartyStateList.ParamsList.Items[0]).CP < 99) then
        AddNewAction (atDrinkGCP, plImmediately);
    if (TParamsController(MainData.PartyStateList.ParamsList.Items[0]).CP < 40) then
        AddNewAction (atDrinkBCP, plImmediately);

//HP
    if (TParamsController(MainData.PartyStateList.ParamsList.Items[0]).HP < 99) then
            AddNewAction (atDrinkWHP, plImmediately);
    if (TParamsController(MainData.PartyStateList.ParamsList.Items[0]).HP < 99) then
            AddNewAction (atDrinkQHP, plImmediately);
    if TParamsController(MainData.PartyStateList.ParamsList.Items[0]).HP < 40 then
    begin
            AddNewAction (atDrinkQHP, plImmediately);
            AddNewAction (atDrinkBHP, plImmediately);
    end;

//MP
    if TParamsController(MainData.PartyStateList.ParamsList.Items[0]).MP < mpLvlLow then
            AddNewAction(atDrinkMP, plImmediately);
end;

procedure TCommonScript.AutoRes;
begin
    KeyController.MouseClick(
    Controller.ResButtonRect.Left,
    Controller.ResButtonRect.Top);
end;

procedure TCommonScript.AnalisData;
begin
    if Main.cbPotions.Checked then SelfControll;
    if Main.cbPvP.Checked then PvPControll;
    if Main.cbRes.Checked then AutoRes;
end;

procedure TCommonScript.AddNewAction (ActionType, Priority: byte; Delay: integer = 0);
var
    i: integer;
begin
    if FActionList.Count < 10 then
    begin
        i := FActionList.Count;
        if i > 0 then
            while TActionObject(FActionList.Items[i - 1]).Priority < Priority do
            begin
                dec (i);
                if i = 0 then break;
            end;
        if Now - FLastUse[ActionType] >= FReUseTime[ActionType] then
            FActionList.Insert(i, TActionObject.Create(ActionType, Priority, Delay));
    end;
end;

function SortByPriority(Item1 : Pointer; Item2 : Pointer) : Integer;
begin
    if TActionObject(Item1).Priority < TActionObject(Item2).Priority then
        Result := 1
        else if TActionObject(Item1).Priority = TActionObject(Item2).Priority then
            Result := 0
            else Result := -1;
end;

procedure TCommonScript.SortActionList;
var
    i: integer;
begin
    for i := 0 to FActionList.Count - 1 do
    begin
        if TActionObject(FActionList.Items[i]).TickCount < 254 then
            TActionObject(FActionList.Items[i]).TickCount :=
                TActionObject(FActionList.Items[i]).TickCount + 1;
        case TActionObject(FActionList.Items[i]).TickCount of
            4..8:    if TActionObject(FActionList.Items[i]).Priority < plNormal then
                            TActionObject(FActionList.Items[i]).Priority := plNormal;
            9..255:   if TActionObject(FActionList.Items[i]).Priority < plHeight then
                            TActionObject(FActionList.Items[i]).Priority := plHeight;
        end;
    end;
    FActionList.Sort(SortByPriority);
end;

procedure TCommonScript.ExecuteActions;
begin
    while FActionList.Count > 0 do
    begin
            TActionObject(FActionList.Items[0]).ExecAction;
            FLastUse[TActionObject(FActionList.Items[0]).FActionType] := Now;
            FActionList.Delete(0);
            sleep(50);
    end;
end;


end.
